import bean.TreeNode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 简单
 *
 * @since 2023-09-17 15:55
 */
public class _104二叉树的最大深度 {
    //深度优先
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        int leftMaxHeight = maxDepth(root.left);
        int rightMaxHeight = maxDepth(root.right);
        return Math.max(leftMaxHeight, rightMaxHeight) + 1;
    }

    //广度优先
    public int maxDepth1(TreeNode root) {
        if(root == null){
            return 0;
        }
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int res = 0, cur = 1, next = 0;
        while(!queue.isEmpty()){
            TreeNode node = queue.poll();
            cur--;
            if(node.left != null){
                queue.offer(node.left);
                next++;
            }
            if(node.right != null){
                queue.offer(node.right);
                next++;
            }
            if(cur == 0){
                cur = next;
                next = 0;
                res++;
            }
        }
        return res;
    }
}
